package com.yuhouxinliang.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuhouxinliang.service.edu.entity.Video;
import com.yuhouxinliang.service.edu.feign.VodMediaService;
import com.yuhouxinliang.service.edu.mapper.VideoMapper;
import com.yuhouxinliang.service.edu.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {


    @Autowired
    private VodMediaService vodMediaService;

    @Override
    public void removeMediaVideoById(String id) {

        log.warn("VideoServiceImpl：video id = " + id);
        //根据videoid找到视频id
        Video video = baseMapper.selectById(id);
        String videoSourceId = video.getVideoSourceId();
        log.warn("VideoServiceImpl：videoSourceId= " + videoSourceId);
        vodMediaService.removeVideo(videoSourceId);
    }

    @Override
    public void removeMediaVideoByChapterId(String chapterId) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper();
        queryWrapper.select("video_source_id");
        queryWrapper.eq("chapter_id", chapterId);
        List<Map<String, Object>> videoList = baseMapper.selectMaps(queryWrapper);
        vodMediaService.removeVideoByIdList(getVideoSourceId(videoList));
    }

    @Override
    public void removeMediaVideoByCourseId(String courseId) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper();
        queryWrapper.select("video_source_id");
        queryWrapper.eq("course_id", courseId);
        List<Map<String, Object>> videoList = baseMapper.selectMaps(queryWrapper);
        vodMediaService.removeVideoByIdList(getVideoSourceId(videoList));
    }

    private List<String> getVideoSourceId(List<Map<String, Object>> videoList) {
        List<String> videoSourceIdList = new ArrayList();
        for (Map<String, Object> stringObjectMap : videoList) {
            String videoSourceIds = (String) stringObjectMap.get("video_source_id");
            videoSourceIdList.add(videoSourceIds);
        }
        return videoSourceIdList;
    }
}
